package zust.se.ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import zust.se.ssm.dto.UserDto;
import zust.se.ssm.form.UserLoginForm;
import zust.se.ssm.form.UserSearchForm;
import zust.se.ssm.model.User;
import zust.se.ssm.service.UserServiceI;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 
 * @author czg
 * @since 2022/12/14 15:57
 */
@Controller
@RequestMapping("users")
// 请求处理方法返回值不允许返回model中对象，只能返回dto中的对象
public class UserController {
    @Autowired
    UserServiceI userService;
    @GetMapping("login")
    public String login(){
        return "login";
    }
    @PostMapping("login")
    public String doLogin(HttpSession session,UserLoginForm form){
        UserDto dto=userService.login(form);
        if(dto==null)
            return "login";
        else{
            session.setAttribute("user",dto);
            return "redirect:/contacts/index";
        }

    }
    @GetMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }
}
